<template>
  <ScrollPage>
    <view class="page-task">
      <view class="task-list">
        <view class="task-title">
          每日任务
        </view>
        <view class="list-container">
          <TaskItem v-for="item in dayTasks" :key="item.id" :task="item" @more="showMore(item)" />
        </view>
      </view>
      <view class="task-list">
        <view class="task-title">
          每周任务
        </view>
        <view class="list-container">
          <TaskItem v-for="item in weekTasks" :key="item.id" :task="item" @more="showMore(item)" />
        </view>
      </view>
      <view class="empty-container" />
    </view>
    <template #bottom>
      <button class="add-btn" @click="show = true">
        添加
      </button>
    </template>
  </ScrollPage>
  <SheetCom v-model="show" :show-remove="!!form.id" :title="form.id ? '编辑任务' : '添加任务'" @submit="submit" @close="reset" @remove="deleteTask">
    <FormItem title="任务名称">
      <wd-input v-model="form.title" placeholder="请输入" />
    </FormItem>
    <FormItem title="任务描述">
      <wd-input v-model="form.description" placeholder="请输入" />
    </FormItem>
    <FormItem title="任务类型">
      <wd-radio-group v-model="form.type" :disabled="!!form.id" checked-color="#84B20F" shape="button">
        <wd-radio v-for="item in taskOptions" :key="item.value" :value="item.value">
          {{ item.label }}
        </wd-radio>
      </wd-radio-group>
    </FormItem>
    <FormItem title="生效子女">
      <wd-checkbox-group v-model="form.children" checked-color="#84B20F" cell shape="button">
        <wd-checkbox v-for="item in childLists" :key="item.id" :model-value="item.id">
          {{ item.name }}
        </wd-checkbox>
      </wd-checkbox-group>
    </FormItem>
    <FormItem title="分数">
      <slider show-value :disabled="!!form.id" :value="form.point" :step="1" :min="1" active-color="#84B20F" :max="100" @change="sliderChange" />
    </FormItem>
  </SheetCom>
</template>

<script lang="ts" setup>
import type { ITask } from '@/api/task'
import { deepClone } from '@alova/shared'
import { storeToRefs } from 'pinia'
import { addTask, removeTask, TaskType } from '@/api/task'
import FormItem from '@/components/FormItem.vue'
import SheetCom from '@/components/Sheet.vue'
import TaskItem from '@/components/TaskItem.vue'
import ScrollPage from '@/layouts/ScrollPage.vue'
import { useChildStore } from '@/store/child'
import { useTaskStore } from '@/store/task'

defineOptions({
  name: 'Task',
})

const show = ref(false)

const defaultData: ITask = {
  title: '',
  description: '',
  type: TaskType.Day,
  children: [],
  point: 1,
}
const form = ref(deepClone(defaultData))

const taskOptions = ref([{
  label: '每日',
  value: TaskType.Day,
}, {
  label: '每周',
  value: TaskType.Week,
}])

definePage({
  style: {
    // 'custom' 表示开启自定义导航栏，默认 'default'
    navigationStyle: 'default',
    navigationBarTitleText: '任务中心',
    backgroundColor: '#fafafa',
  },
})
// #region Props
// #endregion
// #region Emits
// #endregion
// #region Hooks
const { childLists } = useChildStore()
const taskStore = useTaskStore()
const { taskLists } = storeToRefs(taskStore)
// #endregion
// #region Computed
const dayTasks = computed(() => {
  return taskLists.value?.filter(task => task.type === TaskType.Day)
})
const weekTasks = computed(() => {
  return taskLists.value?.filter(task => task.type === TaskType.Week)
})

// #endregion
// #region Watch
// #endregion
// #region Event
// #endregion
// #region Function
function refresh() {
  taskStore.fetchTaskList()
}

function deleteTask(cb: () => void) {
  const { type, title, id } = form.value
  if ((dayTasks.value.length === 1 && type === TaskType.Day) || (weekTasks.value.length === 1 && type === TaskType.Week)) {
    cb && cb()
    return uni.showToast({
      title: `至少保留一个${type === TaskType.Day ? '日' : '周'}任务`,
      icon: 'none',
    })
  }
  uni.showModal({
    content: `确认删除${title}?`,
    success: (res) => {
      if (res.confirm) {
        removeTask({ id }).then(() => {
          refresh()
          reset()
        })
      }
      cb && cb()
    },
  })
}

function sliderChange(e: any) {
  form.value.point = e.detail.value
}

function showMore(item: ITask) {
  form.value = deepClone(item)
  show.value = true
}

function reset() {
  form.value = deepClone(defaultData)
  show.value = false
}

function submit(cb: () => void) {
  if (!form.value.title || !form.value.description) {
    return uni.showToast({
      title: '请填写完整信息',
      icon: 'none',
    })
  }

  addTask({
    ...form.value,
  }).then(() => {
    reset()
    refresh()
  }).finally(() => {
    cb()
  })
}
// #endregion
// #region Life Cycle
// #endregion
// #region Expose
// #endregion
</script>

<style lang="scss" scoped>
.page-task {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 0 20px;
  .task-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
    &:last-child {
      margin-bottom: none;
    }
    .task-title {
      font-weight: 600px;
      color: var(--main-color);
      font-size: 20px;
    }
    .list-container {
      display: flex;
      flex-direction: column;
      gap: 10px;
    }
  }
  .empty-container {
    height: 155px;
    flex: 0 0 auto;
  }
}
.add-btn {
  height: 60px;
  background: var(--button-color);
  margin: 20px 20px 0 20px;
  border-radius: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
